﻿using Inovout.Kuajing.Exports.Models;
using Inovout.Kuajing.Exports.Services;
using Inovout.Organizations.Models;
using Inovout.Organizations.Services;
using Inovout.Web.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Inovout.Kuajing.Exports.Console.Web.Controllers
{

    public class ConsigneeController : KJBaseController<Consignee>
    {
         private IConsigneeService  consigneeService;
         private IOrganizationUnitService organizationUnitService;
         public ConsigneeController(IConsigneeService consigneeService, IOrganizationUnitService organizationUnitService)
        {
            this.consigneeService = consigneeService;
            this.organizationUnitService = organizationUnitService;
        }


        [HttpPost]
        public JsonResult FilterConsigee(string term, string Id)
        {
            OrganizationUnit organizationUnit = null;
            if (Id != "0")
            {
                string OrganizationId = Id;
                organizationUnit = organizationUnitService.FindById(int.Parse(OrganizationId));
            }
            List<Consignee> consigneeList = (List<Consignee>)consigneeService.FindByKeyAndOrganizationUnit(organizationUnit);
            var data = from c in consigneeList
                       select new { value = c.Id, label = c.Address};
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult FindConsigee(string Id)
        {
            OrganizationUnit organizationUnit = null;
            if (Id != "0")
            {
                string OrganizationId = Id;
                organizationUnit = organizationUnitService.FindById(int.Parse(OrganizationId));
            }
            Consignee consignee = consigneeService.FindByOrganizationUnit(organizationUnit);
            if (consignee == null) {
                return null;
            }
            var data = new { id = consignee.Id, address = consignee.Address };
            return Json(data, JsonRequestBehavior.AllowGet);
        }

       
	}
}